#include "DynamicArray.h"
#include <stdio.h>
#include <string.h>

struct Student
{
    int id;
    char *name;
};
typedef struct Student Stu;

Stu *CreateStu(int id, const char *name)
{
    Stu *s = (Stu *)malloc(sizeof(Stu));
    if (s == NULL)
    {
        printf("CreateStu malloc error!\n");
        exit(-1);
    }
    s->id = id;
    s->name = strdup(name); // 堆拷贝名字
    return s;
}

void ClearStu(void *p)
{
    Stu *s = (Stu *)p;
    free(s->name);
    free(s);
    // printf("清理学生\n");
}

// 回调函数
void PrintStu(void *p)
{
    Stu *s = (Stu *)p;
    printf("学生学号：%d  学生姓名：%s\n", s->id, s->name);
}

bool IsNameEqual(void *p1, void *p2)
{
    Stu *s = (Stu *)p1;
    const char *name = (const char *)p2;
    return strcmp(s->name, name)==0;
}

bool IsIDEqual(void *p1, void *p2)
{
    Stu *s = (Stu *)p1;
    long id=(long)p2;
    return s->id==id;
}

void PrintClass(void *p)
{
    DArray *a=(DArray *)p;
    PrintDArray(a,PrintStu);
}


int main1(int argc, char *argv[])
{
    DArray *a = InitDArray();
    // for (int i = 0; i < 30; i++)
    // {
    //     InsertDArray(a,i+1);
    // }
    InsertDArray(a, CreateStu(1, "zhangsan"));
    InsertDArray(a, CreateStu(2, "lisi"));
    InsertDArray(a, CreateStu(3, "wangwu"));
    // void *s = FindByElement(a, (void*)2, IsIDEqual);
    void *s = RemoveDArray(a, (void*)2, IsIDEqual);
    if (s)
    {
        printf("删除的学生是: ");
        PrintStu(s);
        ClearStu(s);
    }
    else
        printf("没找到!\n");
    PrintDArray(a, PrintStu);
    ClearDArray(a, ClearStu);
    return 0;
}
int main2()
{
    DArray *class=InitDArray();
    InsertDArray(class, CreateStu(1, "zhangsan"));
    InsertDArray(class, CreateStu(2, "lisi"));
    InsertDArray(class, CreateStu(3, "wangwu"));

    DArray *class2=InitDArray();
    InsertDArray(class, CreateStu(1, "zhangsan"));
    InsertDArray(class, CreateStu(2, "lisi"));
    InsertDArray(class, CreateStu(3, "wangwu"));

    DArray *school=InitDArray();
    InsertDArray(school,class);
    InsertDArray(school,class2);

    PrintDArray(school,PrintClass);

   
    return 0;
}

void Split(DArray *a,const char *src,const char *delim)
{
    //char *temp=src;
    char *temp=strdup(src);
    char *dest=strtok(temp,delim);
    while(dest !=NULL)
    {
        InsertDArray(a,strdup(dest));
        dest=strtok(NULL,delim);
    }
    free(temp);


}
void PrintString(void *p)
{
    printf("%s\n",(char *)p);
}
int IDcmp(void *p1,void *p2)
{
    int Stu *s1=(Stu *)p1;
    int Stu *s2=(Stu *)p2;
    return s1->id-s2->id;
}
int Namecmp(void *p1,void *p2)
{
    Stu *s1=(Stu *)p1;
    return strcmp(s1->name,s2->name);
}
int main()
{

    DArray *a=InitDArray();
    InsertDArray(a,"zhangsan");
    InsertDArray(a,"lisi");
    InsertDArray(a,"wangwu");
    Split(a,"zhangsan,lisi,wangwu",",");
    PrintDArray(a,PrintString);
    ClearDArray(a,free);

    FAstSort(a,BiggerID);
    
    void *s=FindBySearch(a,(void*)1,IDcmp);
    if(s)
    {
        printf("找到的学生学号是:%d\n",((Stu*)s)->id);
    }
    else
    {
        printf("没找到!\n");
    }
    return 0;
}



